package com.tdkj.service.shop.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.tdkj.dao.shop.IBeanGoodShowcaseDefaultDao;
import com.tdkj.model.shop.BeanGoodShowcaseDefault;
import com.tdkj.service.BaseService;
import com.tdkj.service.shop.IBeanGoodShowcaseDefaultService;
@Component
public class BeanGoodShowcaseDefaultServiceImpl extends BaseService implements
		IBeanGoodShowcaseDefaultService {
	
	private static final int GOOD_SHOWCASE_DEFAULT_QUANTITY = 10;
	
	private IBeanGoodShowcaseDefaultDao beanGoodShowcaseDefaultDao;

	/**
	 * @param beanGoodShowcaseDefaultDao the beanGoodShowcaseDefaultDao to set
	 */
	@Resource
	public void setBeanGoodShowcaseDefaultDao(
			IBeanGoodShowcaseDefaultDao beanGoodShowcaseDefaultDao) {
		this.beanGoodShowcaseDefaultDao = beanGoodShowcaseDefaultDao;
	}

	@Override
	public BeanGoodShowcaseDefault getGoodShowcaseDefault() {
		BeanGoodShowcaseDefault bean = null;
		List<BeanGoodShowcaseDefault> beans = beanGoodShowcaseDefaultDao.findAll(BeanGoodShowcaseDefault.class);
		if(beans != null && beans.size() == 1){
			bean = beans.get(0);
		}else{
			bean = new BeanGoodShowcaseDefault();
			bean.setQuantity(GOOD_SHOWCASE_DEFAULT_QUANTITY);
			beanGoodShowcaseDefaultDao.save(bean);
		}
		return bean;
	}

	@Override
	public void initGoodShowcaseDefault() {
		BeanGoodShowcaseDefault bean = null;
		List<BeanGoodShowcaseDefault> beans = beanGoodShowcaseDefaultDao.findAll(BeanGoodShowcaseDefault.class);
		if(beans != null && beans.size() == 1){
			bean = beans.get(0);
		}else{
			bean = new BeanGoodShowcaseDefault();
			bean.setQuantity(GOOD_SHOWCASE_DEFAULT_QUANTITY);
			beanGoodShowcaseDefaultDao.save(bean);
		}

	}

	@Override
	public void updateGoodShowcaseDefault(Integer quantity) {
		BeanGoodShowcaseDefault bean = null;
		List<BeanGoodShowcaseDefault> beans = beanGoodShowcaseDefaultDao.findAll(BeanGoodShowcaseDefault.class);
		if(beans != null && beans.size() == 1){
			bean = beans.get(0);
		}else{
			bean = new BeanGoodShowcaseDefault();
			bean.setQuantity(GOOD_SHOWCASE_DEFAULT_QUANTITY);
			beanGoodShowcaseDefaultDao.save(bean);
		}
		bean.setQuantity(quantity);
	}

}
